package com.winway.sixbook.ui;

import java.util.ArrayList;
import java.util.HashMap;

import com.winway.sixbook.DeskClockMainActivity;
import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R; 
import com.winway.sixbook.custom.CloseActivityClass;

import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle; 
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SetActivity  extends Activity {
	private static final String TAG = "SetActivity";
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setactivity);
		CloseActivityClass.activityList.add(this);
		//初始化
		mainInit();
	} 
	
	private void mainInit(){
		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
        
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
		top_tbn_fanhui.setOnClickListener(mListener);
        title.setText("种子类型设置");
        
        GridView gridview = (GridView) findViewById(R.id.GridViewSet);
        ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ItemImage", R.drawable.home_classify_zuoye);
        map1.put("ItemText", "种子设置");
        map1.put("ItemTypeid", "1");
        meumList.add(map1);
        
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ItemImage", R.drawable.home_classify_layer_js);
        map2.put("ItemText", "计数设置");
        map2.put("ItemTypeid", "2");
        meumList.add(map2);
        
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("ItemImage", R.drawable.home_classify_layer);
        map3.put("ItemText", "提醒设置");
        map3.put("ItemTypeid", "3");
        meumList.add(map3);
        
        SimpleAdapter saItem = new SimpleAdapter(this,
                  meumList, //数据源
                  R.layout.gridview_item, //xml实现
                  new String[]{"ItemImage","ItemText","ItemTypeid"}, //对应map的Key
                  new int[]{R.id.ItemImage,R.id.ItemText,R.id.Itemtypeid});  //对应R的Id
 
        //添加Item到网格中
        gridview.setAdapter(saItem);
        //添加点击事件
        gridview.setOnItemClickListener(
            new OnItemClickListener()
            {
                @Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
                {
                	TextView ItemText = (TextView) arg1.findViewById(R.id.ItemText);
                	TextView ItemTypeid = (TextView) arg1.findViewById(R.id.Itemtypeid);
                	if("种子设置".equals(ItemText.getText())){
                		Intent counter1 = new Intent(SetActivity.this, SeedChooseActivity.class);
    					startActivity(counter1);
    					finish();
                	}else if("计数设置".equals(ItemText.getText())){
                		Intent counter2 = new Intent(SetActivity.this, SetCounterListActivity.class);
    					startActivity(counter2);
    					finish();
                	}else{
                		Intent counter2 = new Intent(SetActivity.this, DeskClockMainActivity.class);
    					startActivity(counter2);
    					finish();
                	}
                }
            }
        );
	}
	
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.nagivate_btn_left:
					Intent counter = new Intent(SetActivity.this, HomeActivity.class);
					startActivity(counter);
					finish();
					break;
			}
		}
	};
	
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(SetActivity.this, HomeActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
